package com.hsyt.pfc.core.autoconfigure;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.bind.RelaxedPropertyResolver;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.EnvironmentAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

/**
 * @Description:
 * @ClassName: PageHelperAutoConfiguration
 * @Package: com.hsyt.pfc.core.autoconfigure
 * @Author: wangze@hsyuntai.com
 * @Date: 2018年9月19日 10:41
 * @Copyright: 版权归 HSYUNTAI 所有
 */

@Configuration
@EnableConfigurationProperties({PageHelperProperties.class})
public class PageHelperAutoConfiguration implements EnvironmentAware {

    @Autowired
    private PageHelperProperties pageHelperProperties;
    private RelaxedPropertyResolver resolver;

    public PageHelperAutoConfiguration() {
    }

    public void setEnvironment(Environment environment) {
        this.resolver = new RelaxedPropertyResolver(environment, "pagehelper.");
    }

    @Bean
    public PageHelper pageHelper() {
        PageHelper pageHelper = new PageHelper();
        Properties properties = this.pageHelperProperties.getProperties();
        Map<String, Object> subProperties = this.resolver.getSubProperties("");
        Iterator var4 = subProperties.keySet().iterator();

        while (var4.hasNext()) {
            String key = (String) var4.next();
            if (!properties.containsKey(key)) {
                properties.setProperty(key, this.resolver.getProperty(key));
            }
        }

        pageHelper.setProperties(properties);

        return pageHelper;
    }
}

